﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.Gui.Rendering;
using DcsGauges.DataStructures;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using DcsGauges.Input.Processors;

namespace DcsGauges.Gui.Controls
{
    class Button : GuiRenderableBase
    {
        private Texture2D                   background              = null;
        private Texture2D                   hoverBackground         = null;
        private string                      text                    = "";
        private ButtonOrientation           orientation             = ButtonOrientation.Horizationtal;
        private ClickInputProcessor         inputProcessor          = null;

        public enum ButtonOrientation
        {
            Horizationtal, 
            Vertical,
        }

        public Button(GuiRenderableBase parent, URect area, Texture2D background, string text, ButtonOrientation orientation = ButtonOrientation.Horizationtal, Texture2D hoverBackground = null) 
            : base(parent, area)
        {
            this.background             = background;
            this.text                   = text;
            this.hoverBackground        = hoverBackground;
            this.orientation            = orientation;
            this.inputProcessor         = new ClickInputProcessor(this, Input.Events.MouseEvent.MouseButtons.Left);
            this.inputProcessor.Click   += OnInputProcessorClick;
        }

        void OnInputProcessorClick(object sender, EventArgs e)
        {
            this.FireClick();
        }

        public override void Render(SpriteBatch spriteBatch, GraphicsDevice device, Microsoft.Xna.Framework.Rectangle parentRect, float globalZIndex)
        {
            Rectangle viewportRect      = this.CalcViewportRect(parentRect);

            if (orientation == ButtonOrientation.Horizationtal)
            { 
                spriteBatch.Draw(this.background, viewportRect, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, this.NextFrontIndex(globalZIndex, 1));
                spriteBatch.DrawString(FontManager.GetFont(FontManager.DefaultFont), this.text, new Vector2(viewportRect.X, viewportRect.Y), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, this.NextFrontIndex(globalZIndex, 2));
            }
            else
            {
                spriteBatch.Draw(this.background, viewportRect, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, this.NextFrontIndex(globalZIndex, 1));
                spriteBatch.DrawString(FontManager.GetFont(FontManager.DefaultFont), this.text, new Vector2(viewportRect.Left, viewportRect.Bottom), Color.Black, MathEx.Deg2Rad(-90.0f), Vector2.Zero, 1.0f, SpriteEffects.None, this.NextFrontIndex(globalZIndex, 2));
            }
        }

        private void FireClick()
        {
            if (this.Click != null)
            {
                this.Click(this, EventArgs.Empty);
            }
        }

        public override void InjectMouseEvent(Input.Events.MouseEvent evt, GameTime gt)
        {
            this.inputProcessor.InjectMouseEvent(evt, gt);
        }

        public event EventHandler Click;
    }
}
